package com.adream.cliniq.client;

import java.util.List;

import com.adream.cliniq.domain.Person;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class ContactUpdateSc extends Composite {
    private Grid                grid_1;
    private final Button        listButton;
    private final TextBox       lastNameTextBox;
    private final TextBox       firstNameTextBox;
    private ServiceHandlerAsync _serviceHandlerAsync;
    private int                 count = 1;

    public ContactUpdateSc() {

        final DockPanel dockPanel = new DockPanel();
        initWidget(dockPanel);
        dockPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        dockPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

        final HorizontalPanel horizontalPanel = new HorizontalPanel();
        dockPanel.add(horizontalPanel, DockPanel.CENTER);
        horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

        final Button saveButton = new Button();
        horizontalPanel.add(saveButton);
        saveButton.addClickListener(new ClickListener() {
            public void onClick(final Widget sender) {
                doSaveButtonOnClick(sender);
            }
        });
        saveButton.setText("Save");

        final Button cancelButton = new Button();
        horizontalPanel.add(cancelButton);
        cancelButton.setText("Cancel");

        listButton = new Button();
        horizontalPanel.add(listButton);
        listButton.addClickListener(new ClickListener() {
            public void onClick(final Widget sender) {
                doListButtonOnClick(sender);
            }
        });
        listButton.setText("List");

        grid_1 = new Grid();
        dockPanel.add(grid_1, DockPanel.EAST);
        grid_1.resize(1, 3);

        final Grid grid = new Grid();
        dockPanel.add(grid, DockPanel.WEST);
        grid.setCellSpacing(0);
        grid.setCellPadding(0);
        grid.resize(5, 2);

        final Label firstNameLabel = new Label("First Name");
        grid.setWidget(1, 0, firstNameLabel);

        final Label lastNameLabel = new Label("Last Name");
        grid.setWidget(2, 0, lastNameLabel);

        lastNameTextBox = new TextBox();
        grid.setWidget(2, 1, lastNameTextBox);
        lastNameTextBox.addKeyboardListener(new KeyboardListener() {
            public void onKeyDown(final Widget sender, final char keyCode, final int modifiers) {
            }

            public void onKeyPress(final Widget sender, final char keyCode, final int modifiers) {
                doLastNameTextBoxOnKeyPress(sender, keyCode, modifiers);
            }

            public void onKeyUp(final Widget sender, final char keyCode, final int modifiers) {
            }
        });
        lastNameTextBox.setTitle("Please enter the First Name here.");
        lastNameTextBox.setMaxLength(50);
        lastNameTextBox.setText("Last Name");

        firstNameTextBox = new TextBox();
        grid.setWidget(1, 1, firstNameTextBox);
        firstNameTextBox.setMaxLength(50);
        firstNameTextBox.setText("First Name");
    }

    protected void doSaveButtonOnClick(final Widget sender) {
        Person person = new Person();
        person.setFirstName(firstNameTextBox.getText());
        person.setLastName(lastNameTextBox.getText());
        person.setPerson_id(new Long(count++));

        getServiceHandler().handle(person, new AsyncCallback() {

            public void onFailure(Throwable caught) {
                Window.alert("The login.rpc call failed.");
            }

            public void onSuccess(Object result) {
                Window.alert((String) result);
            }
        });
    }

    protected void doListButtonOnClick(final Widget sender) {
        getServiceHandler().list(new AsyncCallback() {

            public void onFailure(Throwable caught) {
                Window.alert("The login.rpc call failed.");
            }

            public void onSuccess(Object result) {
                List people = (List) result;
                Person person;
                int rowCount;
                grid_1.resizeRows(people.size());
                for (rowCount = 0; rowCount < people.size(); rowCount++) {
                    person = (Person) people.get(rowCount);
                    grid_1.setWidget(rowCount, 0, new Label(person.getPerson_id() + ""));
                    grid_1.setWidget(rowCount, 1, new Label(person.getFirstName()));
                    grid_1.setWidget(rowCount, 2, new Label(person.getLastName()));
                }
                count = rowCount + 1;
            }
        });
    }

    private ServiceHandlerAsync getServiceHandler() {
        if (_serviceHandlerAsync == null) {
            _serviceHandlerAsync = (ServiceHandlerAsync) GWT.create(ServiceHandler.class);

            ServiceDefTarget endpoint = (ServiceDefTarget) _serviceHandlerAsync;
            String moduleRelativeURL = GWT.getModuleBaseURL() + "login.rpc";
            endpoint.setServiceEntryPoint(moduleRelativeURL);
        }
        return _serviceHandlerAsync;
    }

    protected void doLastNameTextBoxOnKeyPress(final Widget sender, final char keyCode, final int modifiers) {
    }
}
